package com.currency.mall.ui.web;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.widget.Toast;

import com.currency.mall.R;
import com.currency.mall.widget.MyWebView;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.BridgeWebViewClient;
import com.mifengs.commonbase.base.BaseFragment;
import com.mifengs.commonbase.util.LogUtil;
import com.mifengs.commonbase.util.NetWorkUtils;
import com.mifengs.commonbase.util.ToastUitl;

import org.greenrobot.eventbus.EventBus;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2019/1/3.
 */

public class WebFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, MyWebView.onReceivedTitleListener {

    private static final String TITLE = "title";
    private static final String PATH = "path";
    @BindView(R.id.webview)
    MyWebView webview;
    @BindView(R.id.swipe_container)
    SwipeRefreshLayout swipeContainer;
    private String mTitle;
    private String mPath;

    public static WebFragment newInstance(String path, String title) {
        WebFragment fragment = new WebFragment();
        Bundle args = new Bundle();
        args.putString(TITLE, title);
        args.putString(PATH, path);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mTitle = getArguments().getString(TITLE);
            mPath = getArguments().getString(PATH);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_web, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        swipeContainer.setOnRefreshListener(this);
        swipeContainer.setEnabled(false);
        //Glide.with(this).load(R.drawable.ic_mifeng_progress).into(mWaitingView);
        webview.setWebViewClient(new FundWebViewClient(webview));
        webview.loadUrl(mPath);
        webview.setOnReceivedTitleListener(this);
    }

    @Override
    public void recevied(String title) {
        if (getActivity() instanceof WebViewActivity) {
            if(title.equals(getString(R.string.web_cant_open))){
                ((WebViewActivity) getActivity()).setTitle("");
            }else{
                ((WebViewActivity) getActivity()).setTitle(title);
            }
    }
}

private class FundWebViewClient extends BridgeWebViewClient {

    public FundWebViewClient(BridgeWebView webView) {
        super(webView);
    }

    @Override
    public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
//            super.doUpdateVisitedHistory(view, url, isReload);
//                Toast.makeText(getContext(),"doUpdateVisitedHistory",Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        LogUtil.i("jh", url);
        try {
            url = URLDecoder.decode(url, "UTF-8");
        } catch (UnsupportedEncodingException var4) {
            var4.printStackTrace();
        }

        if (url.startsWith("yy://return/")) {
            webview.handlerReturnData(url);
            return true;
        } else if (url.startsWith("yy://")) {
            webview.flushMessageQueue();
            return true;
        } else {
            return insideLoad(url);
        }
    }


    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);

    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        CookieManager cookieManager = CookieManager.getInstance();
        String cookieStr = cookieManager.getCookie(url);
        if (!TextUtils.isEmpty(cookieStr)) {
            LogUtil.i("cookie", cookieStr);
        }
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return super.shouldOverrideUrlLoading(view, request);
    }

}

    private boolean insideLoad(String url) {
        webview.loadUrl(url);
        return true;
    }

    public MyWebView getWebView() {
        return webview;
    }


    public boolean canGoBack() {
        if(webview!=null){
            return webview.canGoBack();
        }else{
            return false;
        }
    }

    public void goBack() {
        webview.goBack();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onRefresh() {

    }
}
